#include "ColorFilter.hpp"
namespace RoboPioneers::Prometheus::Core
{
	/// 执行方法
	void ColorFilter::Execute()
	{
        cv::GaussianBlur(BGRPicture,BGRPicture,cv::Size(3,3),0.8,0.8);

        cv::Mat hsv_picture(BGRPicture.size(),CV_8UC3,cv::Scalar(0,0,0));
        cv::cvtColor(BGRPicture,hsv_picture, cv::COLOR_BGR2HSV);
        cv::Mat channels[3], hue_masks[2], saturation_masks[2],value_masks[2],
            mask(BGRPicture.size(),CV_8UC1,cv::Scalar(0,0,0));
        cv::split(hsv_picture,channels);
        cv::threshold(channels[0], hue_masks[0], MinHue, 255, cv::THRESH_BINARY);
		cv::threshold(channels[0], hue_masks[1], MaxHue, 255, cv::THRESH_BINARY_INV);
		cv::threshold(channels[1], saturation_masks[0], MinSaturation, 255, cv::THRESH_BINARY);
		cv::threshold(channels[1], saturation_masks[1], MaxSaturation, 255, cv::THRESH_BINARY_INV);
		cv::threshold(channels[2], value_masks[0], MinValue, 255, cv::THRESH_BINARY);
		cv::threshold(channels[2], value_masks[1], MaxValue, 255, cv::THRESH_BINARY_INV);


        cv::Mat hue_mask, saturation_mask, value_mask;
		cv::bitwise_and(hue_masks[0], hue_masks[1], hue_mask, cv::noArray());
		cv::bitwise_and(saturation_masks[0], saturation_masks[1], saturation_mask, cv::noArray());
		cv::bitwise_and(value_masks[0], value_masks[1], value_mask, cv::noArray());

		cv::bitwise_and(hue_mask, saturation_mask, mask, value_mask);
        cv::imshow("Mask",mask);
        cv::morphologyEx(mask,mask,cv::MORPH_CLOSE,Element);
		BinaryPicture=mask.clone();
	}
}